package net.joint_painting.server.model;

import java.util.Collections;
import java.util.logging.Logger;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

/**
 * @author CAM
 * @version $Revision: 1.0 $
 */
public class MemCache {

    private static final Logger log = Logger
	    .getLogger(MemCache.class.getName());
    private static final String key_prefix = "jp";

    /**
     * Method cache.
     * 
     * @param key
     *            String
     * @param data
     *            Object
     */
    public static void cache(String key, Object data) {

	Cache cache = null;

	try {
	    cache = CacheManager.getInstance().getCacheFactory()
		    .createCache(Collections.emptyMap());
	} catch (CacheException e) {
	    log.severe(e.getMessage());
	}
	// Put the value into the cache.
	cache.put(key_prefix + key, data);
    }

    /**
     * Method deleteCached.
     * 
     * @param key
     *            String
     */
    public static void deleteCached(String key) {
	Cache cache = null;

	try {
	    cache = CacheManager.getInstance().getCacheFactory()
		    .createCache(Collections.emptyMap());
	} catch (CacheException e) {
	    log.severe(e.getMessage());
	}
	cache.remove(key_prefix + key);
    }

    /**
     * Method getCached.
     * 
     * @param key
     *            String
     * 
     * @return Object
     */
    public static Object getCached(String key) {
	Cache cache = null;

	try {
	    cache = CacheManager.getInstance().getCacheFactory()
		    .createCache(Collections.emptyMap());
	} catch (CacheException e) {
	    log.severe(e.getMessage());
	}
	// Get the value from the cache.
	return cache.get(key_prefix + key);
    }
}
